package org.finjic.utils;

import org.reflections.Reflections;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Set;

/**
 * User: Taras
 * Date: 02.12.12
 * Time: 12:56
 */
public class ReflectionUtils {
    private static HashMap<String, Reflections> scannedClasses = new HashMap<String, Reflections>();

    public static <T> Set<Class<? extends T>> getSubclasses(String packageName, Class<T> clazz) {
        return getReflections(packageName).getSubTypesOf(clazz);
    }

    public static Set<Class<?>> getTypesAnnotatedWith(String packageName, Class<? extends Annotation> clazz){
        return getReflections(packageName).getTypesAnnotatedWith(clazz);
    }

    private static Reflections getReflections(String packageName) {
        if (scannedClasses.keySet().contains(packageName)) {
            return scannedClasses.get(packageName);
        }
        Reflections reflections = new Reflections(packageName);
        scannedClasses.put(packageName, reflections);
        return reflections;
    }
}
